﻿using EMoney.IStock.Client.Models.HD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.HD
{
    /// <summary>
    /// 投票数据访问类
    /// </summary>
    public class VoteDal
    {
        /// <summary>
        /// 新增投票信息
        /// </summary>
        /// <param name="vote"></param>
        /// <returns></returns>
        public int AddVote(Vote vote)
        {
            string sql = "insert into Tb_bbs_vote(replyid,Userid,Flag,VoteTime) values(@replyid,@Userid,@Flag,getdate())";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("replyid", System.Data.SqlDbType.Int, vote.Replyid);
            tools.AddSqlParameter("Userid", System.Data.SqlDbType.Int, vote.Userid);
            tools.AddSqlParameter("Flag", System.Data.SqlDbType.Int, vote.Flag);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取投票数量
        /// </summary>
        /// <param name="topicID"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public int GetVoteCount(int replyid, int flag)
        {
            string sql = "select count(0) from Tb_bbs_vote where replyid=@replyid and Flag=@Flag";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("replyid", System.Data.SqlDbType.Int, replyid);
            tools.AddSqlParameter("Flag", System.Data.SqlDbType.Int, flag);

            return Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionManager.Emfstock,System.Data.CommandType.Text,sql,tools.GetSqlParameters()));
        }

        /// <summary>
        /// 获取用户对指定提问的投票数量
        /// </summary>
        /// <param name="topicID"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public int GetUserVoteCount(int replyid, int userID)
        {
            string sql = "select count(0) from Tb_bbs_vote where replyid=@replyid and Userid=@Userid";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("replyid", System.Data.SqlDbType.Int, replyid);
            tools.AddSqlParameter("Userid", System.Data.SqlDbType.Int, userID);

            return Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters()));
        }
    }
}
